/**
 * @(#)Test
 * 版权声明 厦门畅享信息技术有限公司, 版权所有 违者必究
 *
 *<br> Copyright:  Copyright (c) 2014
 *<br> Company:厦门畅享信息技术有限公司
 *<br> @author ulyn
 *<br> 14-8-16 下午9:32
 *<br> @version 1.0
 *————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *————————————————————————————————
 */
package cn.ulyn.seckill;

import cn.ulyn.seckill.mockclient.DetailSecKillOrderRequest;
import cn.ulyn.seckill.util.DESede;
import org.apache.commons.codec.binary.Base64;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.TreeMap;

/**
 * <pre></pre>
 * <br>----------------------------------------------------------------------
 * <br> <b>功能描述:</b>
 * <br>
 * <br> 注意事项:
 * <br>
 * <br>
 * <br>----------------------------------------------------------------------
 * <br>
 */
public class Test {

    public static void main(String[] args) throws Exception {
        TreeMap<String, String> params = new TreeMap();
        String baseUrl = "http://api.m.taobao.com/rest/api3.do?";
        DetailSecKillOrderRequest detailSecKillOrderRequest = new DetailSecKillOrderRequest();
        detailSecKillOrderRequest.setQuantity(1);
        detailSecKillOrderRequest.setItemId("dd");
        detailSecKillOrderRequest.setSkuId("skuId");
        detailSecKillOrderRequest.setSid("sid");   //应该是登录后生成的固定的id
        detailSecKillOrderRequest.setDetailValidKeyName("timkn");
        detailSecKillOrderRequest.setDetailValidKeyValue("timk");
        detailSecKillOrderRequest.setEncryptStr("sign"); //问题来的串
        detailSecKillOrderRequest.setAnswer("aaa");
        detailSecKillOrderRequest.setSupportAsync(false);

        params = new TreeMap<String, String>();
        params.put("ecode", "22222222222222222222222");
        params.put("ttid", null);//
        params.put("v", "1.0");
        params.put("t", "1408328323");
        params.put("imei", "860311021335625");
        params.put("api", "mtop.trade.createSeckillOrder");
        params.put("imei", "860311021335625");
        params.put("imsi", "460023803112482");
        params.put("deviceId", "Ahlp6SYwRmi0D4_tvCtyzFiYTA65YXKOltP7DW4PyACg");
        params.put("appKey", "21646297");
        params.put("data", "{\"detailValidKeyValue\":\"dede9fa4a1d69e2282ca6e4794986fc4\",\"detailValidKeyName\":\"9d6c3c55f0a391f2d41b15d9bf9b5189\",\"sid\":\"1599cc1763e258b0fb5375824f8458e4\",\"supportAsync\":false,\"encryptStr\":\"CF50CD72ACB7BA730E107B036AEECF49B29A97FEE3D25094E6490A79BDB5E5FC5E51F798E4E791814FA6E0BF02DA2B72283150FE618724DAADE84BE1496FB91100F81F84BAE206BC537B9A07514FC9C65CBD6853CD8BCD69615810E2ADD9A1E5232CA0FCF95E67C8731F078510A71D401AF1D4ADF76AD0BF\",\"answer\":\"恒安兴\",\"quantity\":1,\"itemId\":\"40461642884\",\"fakeCart\":true}");
        String sig = sign(params, getAppsecret());
        System.out.println(sig);
        params.remove("ecode");

        String st = "{\"detailValidKeyValue\":\"dede9fa4a1d69e2282ca6e4794986fc4\",\"detailValidKeyName\":\"9d6c3c55f0a391f2d41b15d9bf9b5189\",\"sid\":\"1599cc1763e258b0fb5375824f8458e4\",\"supportAsync\":false,\"encryptStr\":\"CF50CD72ACB7BA730E107B036AEECF49B29A97FEE3D25094E6490A79BDB5E5FC5E51F798E4E791814FA6E0BF02DA2B72283150FE618724DAADE84BE1496FB91100F81F84BAE206BC537B9A07514FC9C65CBD6853CD8BCD69615810E2ADD9A1E5232CA0FCF95E67C8731F078510A71D401AF1D4ADF76AD0BF\",\"answer\":\"恒安兴\",\"quantity\":1,\"itemId\":\"40461642884\",\"fakeCart\":true}";
        System.out.println(URLEncoder.encode(st, "utf-8"));

        String ecode = "1234";
        byte[] ecodeBytes = ecode.getBytes("ISO-8859-1");
        String ecodeStr = new String(Base64.encodeBase64(DESede.encryptMode("460023803112482".getBytes(), ecodeBytes)), "ISO-8859-1");
        System.out.println(ecodeStr);
        ecodeStr = "ogH1d+Uk/bw=";
        ecodeBytes = Base64.decodeBase64(ecodeStr.getBytes("ISO-8859-1"));
        String str = new String(DESede.decryptMode("460023803112482".getBytes(), ecodeBytes), "ISO-8859-1");
        System.out.println(str);
    }


    public static String sign(TreeMap<String, String> paramTreeMap, String appsecret) throws Exception {
        String appKey = (String) paramTreeMap.get("appKey");
        String str3 = appsecret;
        String str4 = (String) paramTreeMap.get("api");
        String str5 = (String) paramTreeMap.get("v");
        String str6 = (String) paramTreeMap.get("imei");
        String str7 = (String) paramTreeMap.get("imsi");
        String str8 = (String) paramTreeMap.get("data");
        String str9 = (String) paramTreeMap.get("t");
        String str10 = (String) paramTreeMap.get("ecode");//取不到啊
        String str11 = byte2hex(MessageDigest.getInstance("MD5").digest(appKey.getBytes("utf-8")));
        StringBuffer localStringBuffer = new StringBuffer();
        if ((str10 != null) || ("".equals(str10))) {
            localStringBuffer.append(str10);
            localStringBuffer.append("&");
        }
        localStringBuffer.append(str3);
        localStringBuffer.append("&");
        localStringBuffer.append(str11);
        localStringBuffer.append("&");
        localStringBuffer.append(str4);
        localStringBuffer.append("&");
        localStringBuffer.append(str5);
        localStringBuffer.append("&");
        localStringBuffer.append(str6);
        localStringBuffer.append("&");
        localStringBuffer.append(str7);
        localStringBuffer.append("&");
        localStringBuffer.append(byte2hex(MessageDigest.getInstance("MD5").digest(str8.getBytes("utf-8"))));
        localStringBuffer.append("&");
        localStringBuffer.append(str9);
        try {
            String str1 = byte2hex(MessageDigest.getInstance("MD5").digest(localStringBuffer.toString().getBytes("utf-8")));
            return str1;
        } catch (Exception localException) {
        }
        return "";
    }

    private static String byte2hex(byte[] bytes) {
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                sign.append("0");
            }
            sign.append(hex.toUpperCase());
        }
        return sign.toString();
    }

    public static String getAppsecret() {
        String appsecretSigned = "756h;d8g:429d;57cf&lt;j8g5f:f3:d&lt;d4";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < appsecretSigned.length(); i++) {
            char c = (char) (appsecretSigned.charAt(i) - i % 5);
            sb.append(c);
        }
        return sb.toString();
    }
}

